package com.attack.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.attack.manager.CustomerInfoManager;
import com.attack.manager.InterestInfoManager;
import com.attack.manager.OrderDetailManager;
import com.attack.manager.OrderInfoManager;
import com.attack.manager.ProductManager;
import com.attack.model.OrderInfoModel;
import com.common.component.Pagination;
import com.common.manager.CparamManager;
import com.common.manager.StaffManager;
import com.common.model.StaffInfoModel;
import com.framework.model.IData;
import com.framework.model.impl.DataMap;
import com.framework.web.BaseController;

@Controller
@RequestMapping(value = "/management/order")
public class OrderInfoController extends BaseController{

	 private static String modelUrl="management/order/";
	 
    @Autowired
	private OrderInfoManager orderInfoManager;
    
    @Autowired
	private OrderDetailManager orderDetailManager;
    
    @Autowired
	private CustomerInfoManager customerInfoManager;

    
    @Autowired
	private InterestInfoManager interestInfoManager;
    
    @Autowired
	private StaffManager staffManager;
    
    
    @Autowired
	private CparamManager cparamManager;
    
    @Autowired
	private ProductManager  productManager;
    
    
    /**
     * 功能：页面分页列表
     * @param vo
     * @param param
     * @return
     */
	@RequestMapping("/pageList")
	public ModelAndView pageList(@ModelAttribute("page") Pagination vo,@ModelAttribute("orderInfoModel") OrderInfoModel param) {
		ModelAndView modelView = new ModelAndView(modelUrl+"list");
		List<DataMap> dataList = orderInfoManager.getPageObjectList(vo, param);
		modelView.addObject("vo", vo);
		modelView.addObject("param", param);
		modelView.addObject("objList", dataList);
		return modelView;
	}
	
	
	/**
	 * 功能：初始化添加页面
	 * @param orderInfoModel
	 * @return
	 */
	@RequestMapping("initAdd")
	public ModelAndView initAdd(@ModelAttribute("orderInfoModel") OrderInfoModel orderInfoModel) {
		ModelAndView modelView = new ModelAndView(modelUrl+"add");
		log.info("add is page");
		this.getStaffList(modelView,orderInfoModel);
		cparamManager.getCparamList(modelView, "'SYS_ORDERSOURCE','SYS_ORDERSTATUS','SYS_BANKNAME'");
		this.initProductList(modelView, orderInfoModel.getOrgCode());
		modelView.addObject("vo",orderInfoModel);
		return modelView;
	}
	
	/**
	 * 功能：查询员工信息
	 * @param modelView
	 * @param orgCode
	 */
	private void getStaffList(ModelAndView modelView, OrderInfoModel orderInfoModel){
		IData objMap = new DataMap();
		objMap.put("orgCode", orderInfoModel.getOrgCode());
		//objMap.put("staffNum", orderInfoModel.getStaffNum());
		List staffList = staffManager.getObjectList(objMap);
		modelView.addObject("staffList",staffList);
	}
	
	
	
	/**
	 * 功能：查询员工信息
	 * @param modelView
	 * @param orgCode
	 */
	private void getCustList(ModelAndView modelView, OrderInfoModel orderInfoModel){
		IData objMap = new DataMap();
		objMap.put("orgCode", orderInfoModel.getOrgCode());
		objMap.put("staffNum", orderInfoModel.getStaffNum());
		List staffList = customerInfoManager.getObjectList(objMap);
		modelView.addObject("custList",staffList);
	}
	
	/**
	 * 功能：查询员工信息
	 * @param modelView
	 * @param orgCode
	 */
	private void initProductList(ModelAndView modelView,String orgCode){
		List productList = productManager.getObjectList(new DataMap());
		modelView.addObject("productList",productList);
	}
	
	/**
	 * 功能：初始化编辑页面
	 * @param intID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/{intID}/initEdit")
	public ModelAndView initEdit(@PathVariable("intID") String intID) {
		ModelAndView modelView = new ModelAndView(modelUrl+"update");
		IData dataMap = new DataMap();
		dataMap.put("intID", intID);
		OrderInfoModel orderInfoModel = (OrderInfoModel)orderInfoManager.getObjectInfo(dataMap);
		if(orderInfoModel!=null){
			this.getStaffList(modelView, orderInfoModel);
		}
		cparamManager.getCparamList(modelView, "'SYS_ORDERSOURCE','SYS_ORDERSTATUS','SYS_BANKNAME'");
		this.initProductList(modelView, orderInfoModel.getOrgCode());
		modelView.addObject("vo",orderInfoModel);
		return modelView;
	}
	
	
	
	/**
	 * 功能：显示详情页面
	 * @param intID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/{intID}/showDetail")
	public ModelAndView showDetail(@PathVariable("intID") String intID) {
		ModelAndView modelView = new ModelAndView(modelUrl+"showDetail");
		IData dataMap = new DataMap();
		dataMap.put("intID", intID);
		OrderInfoModel orderInfoModel = (OrderInfoModel)orderInfoManager.getObjectInfo(dataMap);
		if(orderInfoModel!=null){
			this.getStaffList(modelView, orderInfoModel);
		}
		cparamManager.getCparamList(modelView, "'SYS_ORDERSOURCE','SYS_ORDERSTATUS','SYS_BANKNAME'");
		this.initProductList(modelView, orderInfoModel.getOrgCode());
		modelView.addObject("vo",orderInfoModel);
		return modelView;
	}
	
	
	/**
	 * 功能：根据客户ID查询对应的订单信息
	 * @param intID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/showOrderlist")
	public ModelAndView showList(@ModelAttribute("page") Pagination vo,@ModelAttribute("orderInfoModel") OrderInfoModel param) {
		ModelAndView modelView = new ModelAndView(modelUrl+"showOrderlist");
		List<DataMap> orderList = orderInfoManager.getPageObjectList(vo, param);
		modelView.addObject("vo", vo);
		modelView.addObject("custId", param.getCustId());
		modelView.addObject("orderList",orderList);
		return modelView;
	}
	
	/**
	 * 功能：执行插入操作
	 * @param orderInfoModel
	 * @return
	 */
	@ResponseBody
	@RequestMapping("add")
	public Object insert(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("orderInfoModel") OrderInfoModel orderInfoModel) {
		try{
			int opNum = orderInfoManager.insertObjectAndGetID(orderInfoModel);
			if(opNum>0){
				orderDetailManager.updateOrderDetail(orderInfoModel);
				return super.jsonReturn(response,200);
			}else{
				return super.jsonReturn(response,300);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage());
		}
	}
	
	/**
	 * 功能：执行更新操作
	 * @param orderInfoModel
	 * @return
	 */
	@ResponseBody
	@RequestMapping("update")
	public Object update(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("orderInfoModel") OrderInfoModel orderInfoModel) {
		try{
			int opNum =orderInfoManager.updateObject(orderInfoModel);
			if(opNum>0){
				orderDetailManager.updateOrderDetail(orderInfoModel);
				return super.jsonReturn(response,200,true);
			}else{
				return super.jsonReturn(response,300,true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage(),true);
		}
	}
	
	/**
	 * 功能：执行单记录删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/{intID}/delete")
	public Object delete(HttpServletResponse response,@PathVariable("intID") int intID) {
		try{
			IData dataMap = new DataMap();
			dataMap.put("intID", intID);
			int opNum =orderInfoManager.deleteObject(dataMap);
			if(opNum>0){
				return super.jsonReturn(response,200,false);
			}else{
				return super.jsonReturn(response,300,false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage(),false);
		}
	}
	
	/**
	 * 功能：执行批量删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteAll")
	public Object deleteAll(HttpServletResponse response,HttpServletRequest request) {
		String ids = request.getParameter("ids");
		try{
			IData dataMap = new DataMap();
			dataMap.put("ids", ids);
			int opNum =orderInfoManager.deleteObject("deleteAllObject",dataMap);
			if(opNum>0){
				return super.jsonReturn(response,200,false);
			}else{
				return super.jsonReturn(response,300,false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage(),false);
		}
	}
	

	
	/**
	 * 功能：机构人员树
	 * @param response
	 * @param request
	 */
	@ResponseBody
	@RequestMapping("/loadOrderTreeList")
	public void loadOrderTreeList(HttpServletResponse response,HttpServletRequest request){
		StaffInfoModel staffInfoModel = super.getUserSession(request);
		String roleId = staffInfoModel.getRoleids();
		String orderOrgStaffTree = customerInfoManager.loadOrderTree(roleId);
		this.ajaxJson(response, orderOrgStaffTree);
	}
	
	
	
	/**
	 * 功能：提供给订单表使用选择投资客户
	 * @param response
	 * @param request
	 */
	@RequestMapping("/showCustList")
	public ModelAndView showCustList(HttpServletResponse response,HttpServletRequest request){
		ModelAndView modelView = new ModelAndView(modelUrl+"showCustList");
		String staffNum = request.getParameter("staffNum");
		String orgCode = request.getParameter("orgCode");
		String isRelation = request.getParameter("isRelation");
		IData dataMap = new DataMap();
		dataMap.put("staffNum", staffNum);
		dataMap.put("orgCode", orgCode);
		List custList = null;
		if("T".equals(isRelation)){
			custList =  interestInfoManager.getObjectList("getOrderIntegerList",dataMap);
		}else{
			custList = customerInfoManager.getObjectList(dataMap);
			modelView.addObject("objList", custList);
		}
		
		modelView.addObject("staffNum", staffNum);
		modelView.addObject("orgCode", orgCode);
		return modelView;
	}
	
	
	
	/**
	 * 功能：检查合同号是否正常
	 * @param response
	 * @param request
	 */
	@ResponseBody
	@RequestMapping("/checkContractNum")
	public void checkContractNum(HttpServletResponse response,HttpServletRequest request){
		String contractNum = request.getParameter("contractNum");
		boolean isFlag= true;
		JSONObject rtnMsg = new JSONObject();
		if(contractNum!=null){
				try {
					isFlag =  orderInfoManager.checkContractNum(contractNum);
					if(!isFlag){
						rtnMsg.put("msg", "合同号已存在请重新输入");
					}else{
						rtnMsg.put("msg", "校验通过");
					}
				} catch (Exception e) {
					isFlag =false;
					rtnMsg.put("msg", "校验发生异常");
				}
		}else{
			rtnMsg.put("msg", "合同号不能为空");
		}
		rtnMsg.put("isFlag", isFlag);
		this.ajaxJson(response, rtnMsg.toString());
	}
	
}